package com.blog.Bard.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.Bard.base.dao.BardArticleDao;
import com.blog.Bard.base.dao.BardarticleandlabelDao;
import com.blog.Bard.base.pojo.BardArticle;
import com.blog.Bard.base.pojo.Bardarticleandlabel;
import com.blog.Bard.base.pojo.User;
import com.blog.Bard.common.service.BaseServiceImpl;
import com.blog.Bard.common.util.Pager;

@Service
@Transactional
public class BardArticleServiceImpl extends BaseServiceImpl implements BardArticleService {

	@Autowired
	private BardArticleDao bardArticleDao;
	
	@Autowired
	private BardarticleandlabelDao bardarticleandlabelDao;

	@Override
	public BardArticle getOne(Integer id) throws Exception {
		return bardArticleDao.getOne(id);
	}

	@Override
	public void save(BardArticle t, User u) throws Exception {
		Date now = new Date();
		if(t.getId()==null) {			
			t.setArticleStatus(1);
		}
		t.setCreateDate(now);
		t.setUserId(u.getId());
		t.setUsername(u.getName());
		t.setOptDate(now);
		bardArticleDao.save(t);
	}

	@Override
	public void del(Integer id) throws Exception {
		bardArticleDao.deleteById(id);
	}

	@Override
	public Pager pageList(Map<String, Object> params) throws Exception {
		Integer pageNo = (Integer)params.get("pageNo");
		Integer pagesize = (Integer)params.get("pagesize");
		Integer type = (Integer)params.get("type");

		StringBuilder sql = new StringBuilder();
		List<Object> args = new ArrayList<Object>();
		
		sql.append(" select id,articleName,optDate from bardArticle where 1=1 \n");
		
		if(type!=null && type!=0) {
			sql.append(" and id in (select articleId from bardarticleandtype where articleTypeId = ? ) \n");
			args.add(type);
		}
		
		return bardArticleDao.getPagerBySql(sql.toString(), pageNo, pagesize, args.toArray());
	}

	@Override
	public void saveType(Integer id, Integer articleType) throws Exception {
		bardArticleDao.delType(id);
		bardArticleDao.saveType(id, articleType);
	}

	@Override
	public Map<String, Object> getArticleType(Integer id) throws Exception {
		List<Object> args = new ArrayList<Object>();
		args.add(id);
		return bardArticleDao.queryForMap(" select * from bardarticleandtype where articleId = ? ", args.toArray());
	}

	@Override
	public Map<String, Object> getArticleLabels(Integer id) throws Exception {
		List<Object> args = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder();
		
		sql.append(" select l.articleId, group_concat(l.labelId) labelIds \n");
		sql.append(" from bardArticleAndLabel l \n");
		sql.append(" where l.articleId = ? \n");
		sql.append(" group by l.articleId \n");
		
		args.add(id);
		return bardArticleDao.queryForMap(sql.toString(), args.toArray());
	}

	@Override
	public void saveBardarticleandlabel(Bardarticleandlabel[] arr) throws Exception {
		bardArticleDao.delLabels(arr[0].getArticleId());
		for(Bardarticleandlabel t : arr) {
			bardarticleandlabelDao.save(t);
		}
	}
	
}
